import axios from "axios";
import router from '@/router'
import { Message } from "element-ui";
import { BASE_URL } from './common'

//封装公共地址
// axios.defaults.baseURL = 'http://1.15.179.44:3001'
axios.defaults.baseURL = BASE_URL

//请求拦截器加上头部token
axios.interceptors.request.use(config => {
    // console.log(config);

    const token = sessionStorage.getItem('token')

    config.headers.authorization = token

    return config
})

//响应拦截器
axios.interceptors.response.use(res => {
    // console.log(res);
    if(res.data.hasOwnProperty('code') && res.data.code !==0){
        Message.error(res.data.msg)
    }
    return res
},err => {
    console.log('后端出错了');
    if(err.response.data.code === 401){
        router.push('/login')
    }
    return Promise.reject('网络出错！！！')
})


export default axios